Use case: FastTake/ReleaseAll all semaphores at once with correct Key

This use case shows how the application sets a clear key, takes multiple semaphores using the fast-take mechanism, then releases all semaphores associated with the current core in a single operation.

Sequence diagram

HAL1

HAL2

FastTake/ReleaseAll

@startuml
participant "User Application" as App
participant "HSEM Driver" as HSEM

group Set the clear key
    App -> HSEM: HAL_HSEM_SetClearKey(clear_key)
    note right of HSEM
        The semaphore clear key is configured
    end note
end
group Fast take the hardware semaphore 0
    App -> HSEM: HAL_HSEM_FastTake(HSEM_ID_0)
    note right of HSEM
        Hardware semaphore 0 is taken
    end note
end

group Fast take the hardware semaphore 1
    App -> HSEM: HAL_HSEM_FastTake(HSEM_ID_1)
    note right of HSEM
        Hardware semaphore 1 is taken
    end note
end

group get the clear key
    App -> HSEM: clear_key = HAL_HSEM_GetClearKey()
    HSEM --> App: <color:green>Return semaphore clear key App</color>
end


group Release all semaphores
    App -> HSEM: HAL_HSEM_ReleaseAll(clear_key, 1U)
    note right of HSEM
        All semaphores owned by the selected core
        are released in a single operation
    end note
end

group Check semaphore state after release all semaphores
App -> HSEM: HAL_HSEM_IsSemTaken(HSEM_ID_0)
HSEM --> App: 0U
App -> HSEM: HAL_HSEM_IsSemTaken(HSEM_ID_1)
HSEM --> App: 0U
note right of HSEM
The hardware semaphores are released
end note
end
@enduml

FastTake/ReleaseAll

@startuml

participant "User Application" as App
participant "HSEM Driver" as HSEM

group Set the clear key
    App -> HSEM: HAL_HSEM_SetClearKey(clear_key)
    note right of HSEM
        The semaphore clear key is configured
    end note
end

group Fast take the hardware semaphore 0
    App -> HSEM: HAL_HSEM_FastTake(HAL_HSEM_0)
    note right of HSEM
        Hardware semaphore 0 is taken
    end note
end

group Fast take the hardware semaphore 1
    App -> HSEM: HAL_HSEM_FastTake(HAL_HSEM_1)
    note right of HSEM
        Hardware semaphore 1 is taken
    end note
end

group get the clear key
    App -> HSEM: clear_key = HAL_HSEM_GetClearKey()
    HSEM --> App: <color:green>Return semaphore clear key App</color>
end

group Release all semaphores
    App -> HSEM: HAL_HSEM_ReleaseAll(clear_key, HAL_HSEM_LOCKID_CURRENT_CPU)
    note right of HSEM
        All semaphores owned by the current CPU
        are released in a single operation
    end note
end

group Check semaphore state after release
App -> HSEM: HAL_HSEM_IsTaken(HAL_HSEM_0)
HSEM --> App: HAL_HSEM_RELEASED
App -> HSEM: HAL_HSEM_IsTaken(HAL_HSEM_1)
HSEM --> App: HAL_HSEM_RELEASED
note right of HSEM
The hardware semaphores are released
end note
end

@enduml

Code snippet

HAL1

HAL2

uint32_t clear_key;
uint32_t sem_state0, sem_state1;
#define HSEM_ID_0 (0U) /* HW semaphore 0*/
#define HSEM_ID_1 (1U) /* HW semaphore 1*/

/* HW semaphore Clock enable */
__HAL_RCC_HSEM_CLK_ENABLE() ;

HAL_HSEM_SetClearKey(clear_key);

HAL_HSEM_FastTake(HSEM_ID_0);

HAL_HSEM_FastTake(HSEM_ID_1);

clear_key = HAL_HSEM_GetClearKey();

HAL_HSEM_ReleaseAll(clear_key, 1U);

sem_state0 = HAL_HSEM_IsSemTaken(HSEM_SEM_0);

sem_state1 = HAL_HSEM_IsSemTaken(HSEM_SEM_1);

if ((sem_state0 == 0U) && (sem_state1 == 0U))
{
   /* Both hardware semaphores are released */
}
uint32_t clear_key;
hal_hsem_status_t sem_state0, sem_state1;



/* HW semaphore Clock enable */
HAL_RCC_HSEM_EnableClock();

HAL_HSEM_SetClearKey(clear_key);

HAL_HSEM_FastTake(HAL_HSEM_0);

HAL_HSEM_FastTake(HAL_HSEM_1);

clear_key = HAL_HSEM_GetClearKey();

HAL_HSEM_ReleaseAll(clear_key, HAL_HSEM_LOCKID_CURRENT_CPU);

sem_state0 = HAL_HSEM_IsTaken(HAL_HSEM_0);

sem_state1 = HAL_HSEM_IsTaken(HAL_HSEM_1);

if ((sem_state0 == HAL_HSEM_RELEASED) && (sem_state1 == HAL_HSEM_RELEASED))
{
    /* Both hardware semaphores are released */
}